# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=crypto++
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=8.6.0
pkgrel=1
pkgdesc="Crypto++ Library is a free C++ class library of cryptographic schemes (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32'  'clangarm64')
url="https://www.cryptopp.com/"
license=('Boost Software License 1.0')
options=('staticlibs' 'strip' 'emptydirs')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             #"${MINGW_PACKAGE_PREFIX}-doxygen"
             "unzip")
source=("https://www.cryptopp.com/cryptopp${pkgver//./}.zip"
        #https://github.com/weidai11/cryptopp/archive/CRYPTOPP_${pkgver//./_}.tar.gz
        libcrypto++.pc
        CMakeLists.txt
        cryptopp-config.cmake
        )
sha256sums=('20aa413957d9c8ae353ee2f7747bd7ac392f933c60a53e3fd1e41cadbc48d193'
            '60df56dcf354e8e1b24b90d460dc299c6991eb558a7068d154cb4f31be25edbd'
            'b4456217e5260612cca418db43a94644de90dd224d69ebb35a42d3ea3b9a9a40'
            'c9356603bb5963224876a20d135a0a35192c65f7f4153374cee6e5953ff1a764')
noextract=(cryptopp${pkgver//./}.zip)

prepare() {
  [[ -d ${srcdir}/${_realname} ]] && rm -rf ${srcdir}/${_realname}
  unzip ${srcdir}/cryptopp${pkgver//./}.zip -d ${srcdir}/${_realname}
  
  cp -f ${srcdir}/CMakeLists.txt ${srcdir}/${_realname}/
  cp -f ${srcdir}/cryptopp-config.cmake ${srcdir}/${_realname}/

  cd ${srcdir}/${_realname}
  sed -i -e 's/^CXXFLAGS/#CXXFLAGS/' GNUmakefile
}

build() {
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCRYPTOPP_DATA_DIR="" \
      ${extra_config} \
      -DDISABLE_SSSE3=ON \
      -DDISABLE_ASM=ON \
      -DBUILD_DOCUMENTATION=OFF \
      ../${_realname}

  ${MINGW_PREFIX}/bin/cmake --build .
}

check() {
  cd ${srcdir}/build-${MSYSTEM}

  ${MINGW_PREFIX}/bin/cmake --build . --target test
}

package() {
  cd ${srcdir}/build-${MSYSTEM}

  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  # Install pkgconfig file
  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig"
  install -Dm644 "${srcdir}/libcrypto++.pc" "${pkgdir}/${MINGW_PREFIX}/lib/pkgconfig/libcrypto++.pc"
  sed -e "s|@PREFIX@|${MINGW_PREFIX}|g" \
      -e "s|@VERSION@|${pkgver}|g" \
      -i "${pkgdir}/${MINGW_PREFIX}/lib/pkgconfig/libcrypto++.pc"

  # Remove cryptest.exe and test files, only needed for check() and bloats the package
  # because cryptest.exe is linked statically.
  rm "${pkgdir}/${MINGW_PREFIX}/bin/cryptest.exe"
  rm -r "${pkgdir}/${MINGW_PREFIX}/share/cryptopp/"

  install -Dm644 ${srcdir}/${_realname}/License.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
